<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\UserAccount;
use think\Db;

/**
 * Bank逻辑
 */
class Bank extends LogicBase
{
    

    
    /**
     * 获取Bank列表
     */
    public function getBankList($where = [], $field = 'a.*', $order = 'sort desc')
    {
        
        $this->modelBank->alias('a');
        

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $count=$this->modelBank->where($where)->count();
        $this->modelBank->alias('a');
        return ['list'=>$this->modelBank->getList($where, $field, $order),'count'=>$count] ;
    }
    
    /**
     * 获取Bank列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * Bank信息编辑
     */
    public function BankEdit($data = [])
    {
        
        $validate_result = $this->validateBank->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBank->getError()];
        }
        
        $url = url('BankList');

        preg_match_all('/<img.*?src="(.*?)".*?\/?>/i', $data['content'], $out, PREG_PATTERN_ORDER);      //正则匹配img标签的src属性，返回二维数组
        if (!empty($out[1])) {
            foreach ($out[1] as $v) {
                    if(!preg_match('/http/',$v)){
                        $url1 = DMOAIN_URL.$v;
                        $data['content'] = str_replace($v, $url1, $data['content']);   //替换相对路径为绝对路径
                    }

            }
        }

        $result = $this->modelBank->setInfo($data);

        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'Bank' . $handle_text . '，title：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, 'Bank操作成功', $url] : [RESULT_ERROR, $this->modelBank->getError()];
    }

    /**
     * 获取Bank信息
     */
    public function getBankInfo($where = [], $field = 'a.*')
    {
        
        $this->modelBank->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelBank->getInfo($where, $field);
    }
    


    /**

    
    /**
     * Bank删除
     */
    public function BankDel($where = [])
    {
        
        $result = $this->modelBank->deleteInfo($where);
        
        $result && action_log('删除', 'Bank删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'Bank删除成功'] : [RESULT_ERROR, $this->modelBank->getError()];
    }


    //导入excel
    public function excel(){

//       set_time_limit(0);
        $excel = request()->file('excel')->getInfo();//excel为file中的name
        vendor("PHPExcel.PHPExcel.IOFactory");
        $objPHPExcel = \PHPExcel_IOFactory::load($excel['tmp_name']);//读取上传的文件
        $arrExcel = $objPHPExcel->getSheet(0)->toArray();//获取其中的数据

        $word = $arrExcel[0];
        array_shift($arrExcel);

        $num = count($arrExcel) -1;
//        d($arrExcel);
        Db::startTrans();

        foreach ($arrExcel as $key => $value) {
            $data=[
                'username'=>$value[1],
                'nickname'=>$value[1],
                'password'=>$value[1],
            ];

            if($this->modelMember->getInfo(['username'=>$value[1]])){
                continue;
            }
            try{
                if($value[3] && $member=$this->modelMember->getInfo(['username'=>$value[3]])){
                  $data['leader_id']=$member['id'];
                }
                //注册
                $id=\app\common\model\Member::create($data);
                //创建钱包
                UserAccount::addWallet($id->id);
                Db::commit();
            }catch (\Exception $e){
                echo $e->getMessage();
                Db::rollback();

            }

        }



           return  $this->success('导入成功');

    }

}
